package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Depot;
import com.sjlh.centurylianhua.entity.LossesLog;
import com.sjlh.centurylianhua.entity.Product;
import com.sjlh.centurylianhua.entity.ProductType;
import com.sjlh.centurylianhua.mapper.ProductMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {

   @Resource
    private ProductMapper productMapper;

    /**
     * 多条件分页查询
     * @param sc
     * @return
     */
   @RequestMapping("/showsp")
    public Object show(@RequestBody SearchCon sc){
        // 封装条件查询构造器
        QueryWrapper<Product> wrapper = new QueryWrapper<Product>();
        wrapper.like(StringUtils.isnotNull(sc.getName()), "product.name", sc.getName());
        wrapper.eq(StringUtils.isnotNull(sc.getState()), "product.state", sc.getState());
        wrapper.eq(StringUtils.isnotNull(sc.getTypeId()), "product.typeId", sc.getTypeId());
       //追加多表连接的sql语句
        wrapper.apply("product.typeId = producttype.id");
        if(sc.getPage()!= null){
            //代表分页
            Page<Product> productPage=new Page<Product>(sc.getPage(),sc.getLimit());
            //查询   //调用mapper添加的多表连接方法查询
            List<Product> list=productMapper.getInfoPage(productPage,wrapper);
            return new ResultData(true,20000,"成功",list,productPage.getTotal());
        }else {
            // 为空时返回的所有数据
            wrapper.eq( "product.state", 1);
            List<Product> list = productMapper.getInfo(wrapper);
            long count=productMapper.getInfo(wrapper).size();
            return new ResultData(true,20000,"成功",list,count);
        }
    }

    /**
     * 添加商品
     * @param product
     * @return
     */
    @RequestMapping("/add")
    public Object add(@RequestBody Product product){
            int r=productMapper.insert(product);
            if(r==1)
                return new ResultData(true,20000,"添加成功",true,null);
            else
                return new ResultData(false,20000,"添加失败",false,null);

    }

    /**
     * 更新商品
     * @param product
     * @return
     */
    @RequestMapping("/update")
    public Object set(@RequestBody Product product){

            int r = productMapper.updateById(product);
            if(r==1)
                return new ResultData(true,20000,"修改成功",true,null);
            else
                return new ResultData(false,20000,"修改失败",false,null);

    }

    /**
     * 删除商品
     * @param product
     * @return
     */
    @RequestMapping("/del")
    public Object del(@RequestBody Product product){
        int rs = productMapper.deleteById(product.getId());

        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }

    /**
     * 批量删除商品
     * @param ids
     * @return
     */
    @RequestMapping("/batchDel")
    public Object batchDel(String ids) {
        ids=ids.substring(0, ids.length() - 1);
        String[] listgIds = ids.split(",");
        List<Integer> list = new ArrayList<Integer>();
        for (String listStr : listgIds) {
            list.add(Integer.parseInt(listStr));
        }
        int r = productMapper.deleteBatchIds(list);
        if (r != 0) {
            return new ResultData(true,20000,"批量删除成功",true,null);
        } else {
            return new ResultData(false,20001,"批量删除失败",false,null);
        }
    }


}
